#include <iostream>
#include <memory>
#include <sys/socket.h>

#include "Tcpserver.hpp"
#include "Socket.hpp"
#include "Accepter.hpp"
#include "Common.hpp"

using namespace std;



void test(Connection *conn)
{
    std::cout<<"Get event, sockfd is: "<<conn->Sockfd()<<std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "Usage: " << argv[0] << " port\n"
             << endl;
        return 1;
    }

    uint16_t port = std::stoi(argv[1]);

    // 1. 创建 Listensock
    std::unique_ptr<Net_work::Socket> listensock = std::make_unique<Net_work::TcpSocket>();
    listensock->BuildListenSocket(port);
    
    // 2. 创建 tcpserver
    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>();
    std::unique_ptr<Accepter> accepter = std::make_unique<Accepter>();
    
    // 绑定回调方法               //! 非静态的方法需要 取地址 !!!
    auto listen_recver = std::bind(&Accepter::AccepterConnection,accepter.get(),std::placeholders::_1);

    // 3. 构建listen对应的connection
    // 让服务器是 ET 模式
    Connection *conn = ConnectionFactory::\
    BuildListenConnection(listensock->GetSockFd(), listen_recver, EPOLLIN | EPOLLET,svr.get());
                                             // & 传入 回调函数
    svr->AddConnection(conn);
    // 4. 开始事件派发
    svr->Dispatcher();

    return 0;
}